package org.benjamin.lending.service.impl;

import java.sql.Timestamp;
import java.util.Map;
import java.util.Set;

import org.benjamin.lending.domain.entity.InnerMessage;
import org.benjamin.lending.domain.entity.MemberInfo;
import org.benjamin.lending.repository.InnerMessageRepository;
import org.benjamin.lending.service.InnerMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class InnerMessageServiceImpl implements InnerMessageService {
	
	@Autowired
	private InnerMessageRepository innerMessageRepository;

	@Override
	public void sendMessage(String title, String msg, Map<String, Object> context,MemberInfo memberInfo) {
		Set<String> keys = context.keySet();
		for(String key : keys){
			msg = msg.replace(key, context.get(key).toString());
		}
		InnerMessage innerMessage = new InnerMessage();
		innerMessage.setMemberInfo(memberInfo);
		innerMessage.setTitle(title);
		innerMessage.setMsg(msg);
		innerMessage.setSendTime(new Timestamp(System.currentTimeMillis()));
		this.innerMessageRepository.save(innerMessage);
	}

}
